/******************************************************************************
  FILE        : main.c
  PURPOSE     : Reloj binario con interfaz i2c a modulo de 
                tiempo real DS1307
          
  FUNCTIONS   :
  
     
  
  LANGUAGE    : ANSI-C
  ----------------------------------------------------------------------------
  HISTORY 
  2007-01-21 11:30:37 Version Inicial  
******************************************************************************/

#include <hidef.h>      /* for EnableInterrupts macro */
#include <MC68HC908JK3.h>  /* include peripheral declarations */
#include "display.h"
#include "ds1307.h"

// user value for CONFIG1
#define CONFIG1_VAL  0x0F
//     ----------------
//     0,0,0,0, 1,1,1,1
//     | | | |  | | | |
//     | | | |  | | | +-COPD    = 1  disable COP watchdog
//     | | | |  | | +---STOP    = 1  enable STOP instruction
//     | | | |  | +-----SSREC   = 1  short (32 cycle) STOP recovery
//     | | | |  +-------RESERVED
//     | | | +----------LVIPWRD = 1  disable power to LVI system
//     | | +------------RESERVED
//     | +--------------RESERVED
//     +----------------COPRS   = 0  long COP timeout

//if in user-monitor mode, the monitor seeks for the CONFIG1 register value
//at a fixed address (CONFIG1 is write-once)
#define CONFIG1_ADR  0x001F

//instance the flash copy of the CONFIG1 register value
const unsigned char CONFIG1_INIT @CONFIG1_ADR = CONFIG1_VAL;

void init_hardware (void);
void pause (unsigned int time);

/*******************************************************************/
unsigned char     min,hor,sec;

void main(void) {
  init_hardware();
  for(;;) {
    //DISP_refresh();

    sec=DS1307_read(0);   // read second
    min=DS1307_read(1);   // read minute
    hor=DS1307_read(2);   // read hour

    DispSec(sec);
    DispMin(min);
    DispHor(hor);
    
    __RESET_WATCHDOG(); 
  } 
}

void init_hardware (void){
  unsigned char sec;
  DisableInterrupts;
  CONFIG1 =   CONFIG1_VAL;
  pause(20);
  DISP_init();
  DS1307_init();
  sec=DS1307_read(0);
  DS1307_write(0,sec & 0x7F); // enable oscillator(bit 7 =0)
  EnableInterrupts;  
}

void pause (unsigned int time){
 unsigned int i;
 for (i=0;i<time;i++) continue;
}
